#!/usr/bin/env bash
###################################################################################################
# USAGE:
#   Param1: Yocto Dir name, None = current dir
###################################################################################################
WORK_PATH=$(readlink -f ${0} | xargs dirname | xargs dirname)
if [[ "${WORK_PATH}" != $(pwd) ]]; then 
  echo -e "\e[31mPlease execute this command in the following path!!\e[0m"
  echo "${WORK_PATH}"
  exit 1
fi
PROP_DIR=${WORK_PATH}/tool/proprietary
WORK_DIR=${WORK_PATH}/${1}

[ -z "$1" ] && WORK_DIR=${WORK_PATH}/build
# [ -z "$1" ] && WORK_DIR=`pwd`
[ -d ${WORK_DIR} ] || mkdir -p ${WORK_DIR}
[ -e ${WORK_DIR}/.init ] || cp ${WORK_PATH}/tool/init-yocto ${WORK_DIR}/.init
# cp -fr ${WORK_PATH}/tool/localbuild.sh ${WORK_DIR}/localbuild
cp -fr ${WORK_PATH}/tool/bev_pre_result.sh ${WORK_DIR}/bev_pre_result

docker run --rm -it \
    -e http_proxy=${http_proxy} \
    -e https_proxy=${https_proxy} \
    -e no_proxy=${no_proxy} \
    -e HTTP_PROXY=${http_proxy} \
    -e HTTPS_PROXY=${https_proxy} \
    -e NO_PROXY=${no_proxy} \
    -e GENIIE_ID=${GENIIE_ID} \
    -e GENIIE_PASSWORD=${GENIIE_PASSWORD} \
    -v ~:/home/hosthome:rw \
    -v ${PROP_DIR}:/prop:rw \
    -v ${WORK_DIR}:/workdir \
    --name bev-poky-`id -un`-`basename ${WORK_DIR}` \
    --workdir=/workdir \
    art.geniie.net/denso-ten-ci-docker/denso-ten-ci/poky:ubuntu-20.04 \
    bash -c "echo -e '[ -e /workdir/.init ] && . /workdir/.init \
            \nalias bev_pre_result=/workdir/bev_pre_result' \
            >> ~/.bash_profile && exec bash --login -i"
